package org.yoki.edu.portal.web.rest.portal;

import org.yoki.edu.common.protocol.portal.v1.sender.PortalV1PapMsgSender;
import org.yoki.edu.portal.core.radius.domain.Radcheck;
import tk.mybatis.mapper.util.StringUtil;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2017/10/31$ 17:42$
 */
public class PortalWeChatTempLogoutThread extends Thread {

    private static final long tempTime = 60 * 1000; //1分钟

    private String userName;
    private String acIp;
    private Integer acPort;

    public PortalWeChatTempLogoutThread(String userName, String acIp, Integer acPort) {
        this.userName = userName;
        this.acIp = acIp;
        this.acPort = acPort;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(tempTime);
            //休眠后进行判断是否下线ip
            Radcheck radcheck = Radcheck.selectWeChatTypeByUserName(userName);
            if (null != radcheck) {
                if (StringUtil.isEmpty(radcheck.getOpenId())) {
                    int serialNo = (int) (65535 * Math.random());
                    PortalV1PapMsgSender sender = new PortalV1PapMsgSender();
                    try {
                        System.out.println("微信临时账号准备下线，用户名：" + radcheck.getUsername());
                        sender.sendAndParseReqLogout(serialNo, radcheck.getLoginIp(), acIp, acPort);
                        System.out.println("微信临时账号下线成功，用户名：" + radcheck.getUsername());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
